﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VSD.OneStopService.BLL;

public partial class Modules_ChoPhanXuLy : System.Web.UI.Page
{
    private ProfileStoreRepository ProfRepos;
    private List<ProfileStoreRepository.DistributedProfileInfo> ListProfiles;
    private bool IsManager = false;
    private bool IsDirector = false;
    private bool IsViewAll = false;
    private int CurrentDeptID = -1;

    public int start = 0;
    int pageSize = 15;
    int pageSegSize = 1;
    int count = 0;

    public string searchAllProf
    {
        set;
        get;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ContextUser.PermissionCollection != null)
        {
            IsManager = ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_PHONG_CHUYENMON);
            IsDirector = ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_COQUAN); //Cho phep quan tri co quyen nhu lanh dao co quan
            IsViewAll = ContextUser.PermissionCollection.HasPermission(PermissionCode.XEM_TOANBO_HOSO);
        }

        if (ContextUser.CurrentUser != null && ContextUser.CurrentUser.DepartmentID.HasValue)
            CurrentDeptID = ContextUser.CurrentUser.DepartmentID.Value;
        else CurrentDeptID = -1;
        txtSearchProfile.Focus();

        if (!IsPostBack)
        { }
    }

    public void Page_PreRender(object sender, EventArgs e)
    {
        FilterData();
    }

    public void FilterData()
    {
        ProfRepos = new ProfileStoreRepository();
        ListProfiles = ProfRepos.GetWaitForDistributedProfiles(0, start, pageSize, out count).ToList();
        searchAllProf = txtSearchProfile.Text.ToString().ToLower();

        if (this.ListProfiles == null) return;

        if (string.IsNullOrWhiteSpace(searchAllProf))
        {
            rptProfiles.DataSource = ListProfiles;
            rptProfiles.DataBind();
        }
        else
        {
            ListProfiles = ListProfiles.FindAll(x =>
                     (x.ServiceName != null && x.ServiceName.ToLower().Contains(searchAllProf.ToLower()))
                     || (x.SubscriberName != null && x.SubscriberName.ToLower().Contains(searchAllProf.ToLower()))
                     || (x.ProfileCode != null && x.ProfileCode.ToLower().Contains(searchAllProf.ToLower()))
                     ).ToList();

            rptProfiles.DataSource = ListProfiles;
            rptProfiles.DataBind();
        }

        AspNetPager1.RecordCount = count;
        AspNetPager1.PageSize = pageSize;
        // AspNetPager1.CurrentPageIndex = 1;
        if (AspNetPager1.PageCount <= 1) divPagination.Visible = false;
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        start = AspNetPager1.StartRecordIndex - 1;
        try
        {
            ProfileStoreRepository resProfileStore = new ProfileStoreRepository();
            ListProfiles = ProfRepos.GetWaitForDistributedProfiles(0, start, pageSize, out count).ToList();
            searchAllProf = txtSearchProfile.Text.ToString().ToLower();
            if (ListProfiles == null) return;

            if (string.IsNullOrWhiteSpace(searchAllProf))
            {
                this.rptProfiles.DataSource = ListProfiles;
                rptProfiles.DataBind();
            }
            else
            {
                ListProfiles = ListProfiles.FindAll(x =>
                    (x.ServiceName != null && x.ServiceName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.SubscriberName != null && x.SubscriberName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.ProfileCode != null && x.ProfileCode.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.DeptName != null && x.DeptName.ToLower().Contains(searchAllProf.ToLower()))

                    ).ToList();

                this.rptProfiles.DataSource = ListProfiles;
                rptProfiles.DataBind();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
}